const fs = require('fs-extra')
const R = require('ramda')
const {booksFile} = require('./config')

const toString = history => history.reduce((acc, h) => {
  return acc + h.price + ' -> '
}, '')

const changed = async () => {
  const booksInFile = await fs.readJson(booksFile)
  const list = R.pipe(
    R.sort((a, b) => a.id - b.id),
    R.filter(a => a.history && a.history.length > 0),
    R.forEach(b => {
      console.log(`${b.id} ${b.title} ${b.author} ${b.grade_info} ${b.num_articles}`)
      console.log(`${toString(b.history)}${b.price}`)
    })
  )
  list(booksInFile)
}

module.exports = changed
